package com.logica.tutorial.web;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
import org.apache.wicket.ajax.attributes.ThrottlingSettings;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.util.time.Duration;

public class UpdateZipCodesBehaviorDelayed extends AjaxFormComponentUpdatingBehavior {
	private HomePage homePage;
	private WebMarkupContainer zipCodePanel;

	public UpdateZipCodesBehaviorDelayed(String event, HomePage homePage,
			WebMarkupContainer zipCodePanel) {
		super(event);
		this.homePage = homePage;
		this.zipCodePanel = zipCodePanel;
	}

	@Override
	protected void onUpdate(AjaxRequestTarget target) {
		zipCodePanel.setVisible(true);
		homePage.updateZipCodeList();
		target.add(zipCodePanel);
	}

	@Override
	protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
		super.updateAjaxAttributes(attributes);

		attributes.setThrottlingSettings(new ThrottlingSettings("throttle",
				Duration.ONE_SECOND, true));
	}
}
